package com.project.sianet.data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.project.sianet.R;
import com.project.sianet.entidades.*;
import com.project.sianet.util.*;

public class ProfesorArrayAdapter extends ArrayAdapter<Profesor> {

	private static final String tag = "ProfesorArrayAdapter";
	private Activity activity;
    private Context context;
    private ImageView foto;
    private TextView login;
    private TextView nombres;
    private TextView apellidos;
    private TextView cursos;
    private List<Profesor> profesores = new ArrayList<Profesor>();
    private SharedPreferences prefs;
    private String host;
    
    
    public ProfesorArrayAdapter(Activity activity, int textViewResourceId,
            List<Profesor> objects) {
        super(activity, textViewResourceId, objects);
        this.activity = activity;
        this.context = activity.getApplicationContext();;
        this.profesores = objects;
        prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
        host = context.getString(R.string.pref_host_default) + prefs.getString("ColegioPF", "");
    }
    
    public int getCount() {
        return this.profesores.size();
    }
 
    public Profesor getItem(int index) {
        return this.profesores.get(index);
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            // ROW INFLATION
            Log.d(tag, "Starting XML Row Inflation ... ");
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.profesores_list, parent, false);
            Log.d(tag, "Successfully completed XML Row Inflation!");
        }
 
        Profesor prof = getItem(position);
         
        foto = (ImageView) row.findViewById(R.id.prof_foto);
        apellidos = (TextView) row.findViewById(R.id.prof_apellidos);
        nombres = (TextView) row.findViewById(R.id.prof_nombres);
        cursos = (TextView) row.findViewById(R.id.prof_cursos);
        login = (TextView) row.findViewById(R.id.prof_login);
 
        apellidos.setText(prof.getApellidos());
        nombres.setText(prof.getNombres());
        
        List<String> lcursos = Arrays.asList(prof.getCursos().split("\\s*,\\s*"));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lcursos.size(); i++) {
			sb.append("- " + lcursos.get(i).toString());
			if (i < lcursos.size() -1)
				sb.append(Html.fromHtml("<br />"));
		}
        
        cursos.setText(sb.toString());
        login.setText(prof.getLogin());

        ImageLoader imgLoader = new ImageLoader(activity);
        imgLoader.DisplayImage(host + "/foto.ashx?login=" + prof.getLogin(), foto);
        
        return row;
    }
	
}
